package com.zhihaoliang.coroutine.buss.flow.func

import com.zhihaoliang.coroutine.util.log
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch
import kotlin.coroutines.coroutineContext

/**
 * @author 支豪亮
 * @date 2022/10/24 14:59
 * @des Flow builders : flowOf and asFlow
 */
object FlowBuilder {
    fun onFlowBuilder(mainScope: CoroutineScope) = mainScope.launch {
        (1..3).asFlow().collect { value -> log(value) }

        listOf(1,2,3,4).asFlow().collect { value -> log(value) }

        simple().collect{ value -> log(value) }
    }

    private fun simple(): Flow<Int> = flow {
        log("Flow started", coroutineContext)
        for (i in 1..3) {
            delay(100)
            emit(i)
        }
    }
}